package com.yami.shop.mystery.common.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.mystery.common.dao.ActivityPresellQualificationMapper;
import com.yami.shop.mystery.common.model.ActivityPresellQualification;
import com.yami.shop.mystery.common.service.ActivityPresellQualificationServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class ActivityPresellQualificationServerImpl extends ServiceImpl<ActivityPresellQualificationMapper, ActivityPresellQualification> implements ActivityPresellQualificationServer {

    @Autowired
    private ActivityPresellQualificationMapper activityPresellQualificationMapper;

    @Override
    public Long getQualificationCount(Long prodId, String userId,Long type ) {
        QueryWrapper<ActivityPresellQualification> activityPresellQualificationQueryWrapper = new QueryWrapper<>();
        if(type == 1){
            activityPresellQualificationQueryWrapper.and(qw->qw.eq("prod_id",prodId).or().eq("type","2"));
        }else {
            activityPresellQualificationQueryWrapper.and(qw->qw.eq("box_id",prodId).or().eq("type","2"));
        }
        activityPresellQualificationQueryWrapper.eq("user_id",userId);
        activityPresellQualificationQueryWrapper.eq("status",0);
        activityPresellQualificationQueryWrapper.gt("times",0);

//        activityPresellQualificationQueryWrapper.and(qw->qw.gt("effective_time",new Date()).or().isNull("effective_time"));
        List<ActivityPresellQualification> activityPresellQualifications = activityPresellQualificationMapper.selectList(activityPresellQualificationQueryWrapper);
        return ObjectUtil.isEmpty(activityPresellQualifications) ? 0L : activityPresellQualifications.stream().mapToLong(ActivityPresellQualification::getTimes).sum();
    }


    @Override
    public void addTimesBySourceId(long personRightsId, long times) {
        LambdaUpdateWrapper<ActivityPresellQualification> activityPresellQualificationLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        activityPresellQualificationLambdaUpdateWrapper.setSql("times = times+"+times);
        activityPresellQualificationLambdaUpdateWrapper.eq(ActivityPresellQualification::getId,personRightsId);
        this.update(activityPresellQualificationLambdaUpdateWrapper);
    }

    @Override
    public void startAssetTransfer() {
        // 查询过期的权益
        log.info("权益过期处理");
        QueryWrapper<ActivityPresellQualification> activityPresellQualificationQueryWrapper = new QueryWrapper<>();
        activityPresellQualificationQueryWrapper.lt("effective_time",new Date());
        activityPresellQualificationQueryWrapper.eq("status",0);
        List<ActivityPresellQualification> activityPresellQualifications = activityPresellQualificationMapper.selectList(activityPresellQualificationQueryWrapper);
        log.info("查询到过期的权益一共有{}条",activityPresellQualifications.size());
        // 修改状态
        activityPresellQualifications.forEach(s->{
            s.setStatus(3L);
        });
        this.updateBatchById(activityPresellQualifications);
    }
}
